﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Lead2012.Business
{
    public class FieldGroupRepository
    {        
        // Create an instance of entity
        Lead2012Entities entities = new Lead2012Entities();

        public List<FieldGroup> GetAllByParentId(int parentId)
        {
            return (from c in entities.FieldGroups 
                    where c.ParentId == parentId || (c.ParentId == null && parentId == 0) 
                    orderby c.OrderId ascending
                    select c).ToList();
        }

        public FieldGroup GetSingle(int id)
        {
            return (from _object in entities.FieldGroups where _object.GroupId == id select _object).FirstOrDefault();
        }

        public void Create(FieldGroup _object)
        {
            entities.AddToFieldGroups(_object);
            entities.SaveChanges();
        }

        public void Update(FieldGroup _object)
        {
            entities.SaveChanges();
        }

        public void Delete(int id)
        {
            var _object = (from _list in entities.FieldGroups where _list.GroupId == id select _list).First();
            entities.DeleteObject(_object);
            entities.SaveChanges();
        }
    }
}